﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Xml;
using System.Data.SqlClient;
using System.Data;
using System.IO;


public partial class kecheng_mp_upLoad : System.Web.UI.Page
{
    public string url = "";
    string xmlPath = "/lapUpLoad/temp/";
    public SqlCommand com;
    public int title_id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["gConnectionString"]);
        conn.Open();
        com = new SqlCommand();
        com.Connection = conn;

        if (Request.QueryString["title_id"] != null)
        {
            title_id = Convert.ToInt32(Request.QueryString["title_id"]);
            if (!IsPostBack)
            {
                Session["xmlFileName"]=DateTime.Now.ToFileTime().ToString();
                url = System.Configuration.ConfigurationManager.AppSettings["php"] + string.Format("index.php?xmlName={0}&config={1}", Session["xmlFileName"].ToString(), 1);
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('title_id is null!');</script>");
        }
    }

    protected void Button9_Click(object sender, EventArgs e)
    {
        Session["xmlFileName"] = DateTime.Now.ToFileTime().ToString();
        url = System.Configuration.ConfigurationManager.AppSettings["php"] + string.Format("index.php?xmlName={0}&config={1}", Session["xmlFileName"].ToString(), 1);
        Button10.Enabled = false;
    }

    protected bool readXmlFile(string path)
    {
        //遍历xml文件
        XmlDocument xmlDoc = new XmlDocument();
        if (System.IO.File.Exists(path))
        {
            xmlDoc.Load(path);
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("track");
            foreach (XmlNode item in nodeList)
            {
                string fileName =Path.GetFileNameWithoutExtension( item.SelectSingleNode("name").InnerText)+".flv";
                string trueName = item.SelectSingleNode("trueName").InnerText;
                string sortId = item.SelectSingleNode("sortid").InnerText; 
                insertToDb(fileName, trueName, sortId);
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    bool insertToDb(string fileName, string trueName,string sortid)
    {
        //将xml文件内的数据插入的数据库中
        string sql = "insert into imgUpLoad(title_id,f_fileName,f_trueName,innerOrder,convertStatus) values(@title_id,@f_fileName,@f_trueName,@innerOrder,@convertStatus)";
        com.CommandType = CommandType.Text;
        com.CommandText = sql;
        com.Parameters.Clear();
        com.Parameters.AddWithValue("@title_id", title_id);
        com.Parameters.AddWithValue("@f_fileName", fileName);
        com.Parameters.AddWithValue("@f_trueName", trueName);
        com.Parameters.AddWithValue("@innerOrder", sortid);
        com.Parameters.AddWithValue("@convertStatus", 1);//代码意义：0不可转换1未转换2转换成功3转换失败
        int result = com.ExecuteNonQuery();
        
        return true;
    }
    protected void Button10_Click(object sender, EventArgs e)
    {
        string path = MapPath("../") + xmlPath + Session["xmlFileName"].ToString() + ".xml";
        if (System.IO.File.Exists(path))
        {
            if (readXmlFile(path))
            {
                Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "提示：此次上传成功！"));
                Button9.Enabled = true;
                handleFiles();
            }
            else
            {
            }
        }
        else
        {
            Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "提示：你还没有上传文件！"));
            url = System.Configuration.ConfigurationManager.AppSettings["php"] + string.Format("index.php?xmlName={0}&config={1}", Session["xmlFileName"].ToString(), 1);
        }
    }

    protected void handleFiles()
    {
        string temp = MapPath("../lapUpLoad/temp");
        DirectoryInfo dir = new DirectoryInfo(temp);
        FileInfo[] files=dir.GetFiles("*.flv");
        string destinationPath = MapPath("../lapUpLoad/flv");
        foreach (var item in files)
        {
          item.MoveTo(Path.Combine(destinationPath, item.Name));
        }
    }

}
